package com.chainyoung.component_tmperson.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.common.view.PinView;
import com.chainyoung.common.view.kprogresshud.KProgressHUD;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SetPinCodeActivity extends BaseActivity {


    @BindView(R2.id.pinView)
    PinView pinView;

    private boolean isReset;
    private boolean isConfirmReset;
    private String pinCode;
    private boolean isImportWallet;
    private boolean isCreateWallet;

    private String coinType;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected void initUI() {
        isReset = getIntent().getBooleanExtra("isReset", false);
        isConfirmReset = getIntent().getBooleanExtra("isConfirmReset", false);
        isImportWallet = getIntent().getBooleanExtra("isImportWallet", false);
        isCreateWallet = getIntent().getBooleanExtra("isCreateWallet", false);
        coinType = getIntent().getStringExtra("coinType");
        pinCode = getIntent().getStringExtra("pinCode");
        setCurrentTitle(isReset ? R.string.tmperson_reset_pin_code : R.string.tmperson_set_pin);
        pinView.setPinViewEventListener(new PinView.PinViewEventListener() {
            @Override
            public void onDataEntered(final PinView pinview, boolean fromUser) {
                if (isReset) {
                    closeKeybord();
                    Intent intent = new Intent(mContext, SetPinCodeActivity.class);
                    intent.putExtra("isConfirmReset", true);
                    intent.putExtra("pinCode", pinview.getValue());
                    mContext.startActivity(intent);
                    finish();
                } else if (isConfirmReset) {
                    if (pinView.getValue().equals(pinCode)) {
                        closeKeybord();
                        ToastUtil.showToast("设置成功");
                        SPUtil.put(LibConstant.PIN_CODE, pinview.getValue());
                        finish();
                    } else {
//                        ToastUtil.showToast("两次密码不一致");
//                        pinview.clearValue();
                        KProgressHUD.create(mContext)
                                .setCustomView(mContext, R.drawable.tips_error)
                                .setLabel(getString(R.string.tmperson_pincode_unsame))
                                .show().setDuration(1500);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                pinview.clearValue();
                            }
                        }, 1500);
                        return;
                    }
                } else {
                    closeKeybord();
                    Intent intent = new Intent(mContext, ConfirmPinCodeActivity.class);
                    intent.putExtra("pinCode", pinview.getValue());
                    intent.putExtra("isImportWallet", isImportWallet);
                    intent.putExtra("isCreateWallet", isCreateWallet);
                    intent.putExtra("coinType", coinType);
                    mContext.startActivity(intent);
                    finish();
                }
            }
        });

    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_activity_set_pin_code;
    }


    @Override
    public void onLeftViewClicked(View view) {
        closeKeybord();
        super.onLeftViewClicked(view);
    }

    /**
     * 关闭软键盘
     */
    public void closeKeybord() {
        InputMethodManager imm = (InputMethodManager) mContext
                .getSystemService(Context.INPUT_METHOD_SERVICE);

        if (imm != null) {
            imm.hideSoftInputFromWindow(pinView.getWindowToken(), 0);
        }
    }

    @Override
    protected void onPause() {
        closeKeybord();
        super.onPause();
    }
}
